/*-------------------------------------------------------------------------
 *
 * config_info.c
 *		用于pg_config输出的公共代码
 *
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  src/common/config_info.c
 *
 *-------------------------------------------------------------------------
 */

#ifndef FRONTEND
#include "postgres.h"
#else
#include "postgres_fe.h"
#endif

#include "common/config_info.h"


/*
 * get_configdata(const char *my_exec_path, size_t *configdata_len)
 *
 * 获取配置时常量。调用者负责释放结果。
 */
ConfigData *
get_configdata(const char *fc_my_exec_path, size_t *fc_configdata_len)
{
	ConfigData *fc_configdata;
	char		fc_path[MAXPGPATH];
	char	   *fc_lastsep;
	int			fc_i = 0;

	/* 根据下面填写的项目数量调整此项 */
	*fc_configdata_len = 23;
	fc_configdata = (ConfigData *) palloc(*fc_configdata_len * sizeof(ConfigData));

	fc_configdata[fc_i].name = pstrdup("BINDIR");
	strlcpy(fc_path, fc_my_exec_path, sizeof(fc_path));
	fc_lastsep = strrchr(fc_path, '/');
	if (fc_lastsep)
		*fc_lastsep = '\0';
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("DOCDIR");
	get_doc_path(fc_my_exec_path, fc_path);
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("HTMLDIR");
	get_html_path(fc_my_exec_path, fc_path);
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("INCLUDEDIR");
	get_include_path(fc_my_exec_path, fc_path);
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("PKGINCLUDEDIR");
	get_pkginclude_path(fc_my_exec_path, fc_path);
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("INCLUDEDIR-SERVER");
	get_includeserver_path(fc_my_exec_path, fc_path);
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("LIBDIR");
	get_lib_path(fc_my_exec_path, fc_path);
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("PKGLIBDIR");
	get_pkglib_path(fc_my_exec_path, fc_path);
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("LOCALEDIR");
	get_locale_path(fc_my_exec_path, fc_path);
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("MANDIR");
	get_man_path(fc_my_exec_path, fc_path);
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("SHAREDIR");
	get_share_path(fc_my_exec_path, fc_path);
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("SYSCONFDIR");
	get_etc_path(fc_my_exec_path, fc_path);
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("PGXS");
	get_pkglib_path(fc_my_exec_path, fc_path);
	strlcat(fc_path, "/pgxs/src/makefiles/pgxs.mk", sizeof(fc_path));
	cleanup_path(fc_path);
	fc_configdata[fc_i].setting = pstrdup(fc_path);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("CONFIGURE");
	fc_configdata[fc_i].setting = pstrdup(CONFIGURE_ARGS);
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("CC");
#ifdef VAL_CC
	fc_configdata[fc_i].setting = pstrdup(VAL_CC);
#else
	fc_configdata[i].setting = pstrdup(_("not recorded"));
#endif
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("CPPFLAGS");
#ifdef VAL_CPPFLAGS
	fc_configdata[fc_i].setting = pstrdup(VAL_CPPFLAGS);
#else
	fc_configdata[i].setting = pstrdup(_("not recorded"));
#endif
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("CFLAGS");
#ifdef VAL_CFLAGS
	fc_configdata[fc_i].setting = pstrdup(VAL_CFLAGS);
#else
	fc_configdata[i].setting = pstrdup(_("not recorded"));
#endif
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("CFLAGS_SL");
#ifdef VAL_CFLAGS_SL
	fc_configdata[fc_i].setting = pstrdup(VAL_CFLAGS_SL);
#else
	fc_configdata[i].setting = pstrdup(_("not recorded"));
#endif
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("LDFLAGS");
#ifdef VAL_LDFLAGS
	fc_configdata[fc_i].setting = pstrdup(VAL_LDFLAGS);
#else
	fc_configdata[i].setting = pstrdup(_("not recorded"));
#endif
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("LDFLAGS_EX");
#ifdef VAL_LDFLAGS_EX
	fc_configdata[fc_i].setting = pstrdup(VAL_LDFLAGS_EX);
#else
	fc_configdata[i].setting = pstrdup(_("not recorded"));
#endif
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("LDFLAGS_SL");
#ifdef VAL_LDFLAGS_SL
	fc_configdata[fc_i].setting = pstrdup(VAL_LDFLAGS_SL);
#else
	fc_configdata[i].setting = pstrdup(_("not recorded"));
#endif
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("LIBS");
#ifdef VAL_LIBS
	fc_configdata[fc_i].setting = pstrdup(VAL_LIBS);
#else
	fc_configdata[i].setting = pstrdup(_("not recorded"));
#endif
	fc_i++;

	fc_configdata[fc_i].name = pstrdup("VERSION");
#ifdef FDD
	fc_configdata[fc_i].setting = pstrdup(PG_VERSION_STR);
#else
	fc_configdata[fc_i].setting = pstrdup("PostgreSQL " PG_VERSION);
#endif
	fc_i++;

	Assert(fc_i == *fc_configdata_len);

	return fc_configdata;
}
